package cn.com.dhcc.ddi.stat.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.service.CmUserMgrService;
import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.vo.UserInfoVo;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.ddi.datamapping.service.DataMappingService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.util.fusioncharts.Categories;
import cn.com.dhcc.app.pub.core.util.fusioncharts.Category;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartDataset;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartSet;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartXmlUtil;
import cn.com.dhcc.ddi.stat.service.TaskSyncStatService;
import cn.com.dhcc.ddi.stat.vo.TaskSyncStatVo;

/**
 * 统计controller
 * @日期：16 Jan 201717:22:40
 * @作者：李博宇
 * @版权所有：东华软件
 * @版本：1.0
 */

@Controller
@RequestMapping("/stat")
public class StatisticsController extends BaseController{
	
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	@Autowired
	TaskSyncStatService taskSyncStatService;
	@Autowired
	CmUserMgrService userMgrService;
	@Autowired
	DataMappingService dataMappingService;
	
	@RequestMapping("task_stat")
	public String taskStat(Model model,HttpSession httpSession){		
		/*try {
			WhereCondition condition = new WhereCondition();
			PagerData<TaskSyncStatVo> taskPagerData = taskSyncStatService.getTaskStatPagerData(pager, condition, dateScope, date);
			model.addAttribute("table_data", taskPagerData.getDatas());
		} catch (ServiceException e) {
			logger.error(e);
		}*/
		return "stat/task_stat";
	}
	
	@RequestMapping(value="/get_state_table", method = RequestMethod.POST)
	public String taskStatTableView(Model model, Pager pager, int dateScope, String date, int offSet) {	
		try {
			pager = new Pager(offSet, 10);
			WhereCondition condition = new WhereCondition();
			PagerData<TaskSyncStatVo> taskPagerData = taskSyncStatService.getTaskStatPagerData(pager, condition, dateScope, date);
			model.addAttribute("data", taskPagerData.getDatas());
			setTotoal(model, taskPagerData.getTotal());
		} catch (ServiceException e) {
			logger.error(e);
		}
		
		return "stat/task_stat_table";
	}
	
	@RequestMapping(value="/task_chart_data", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> generateTaskData(int dateScope, int dataScope, String date,HttpSession httpSession) {
		try {
			WhereCondition taskParameter = new WhereCondition();
			Boolean isAdmin = BaseController.getCurrentUserInfo(httpSession).isSuperAdmin();
			String userId = BaseController.getCurrentUserInfo(httpSession).getId();
			taskParameter.groupBy("task_id");
			if(!isAdmin){
				UserInfoVo currentUser = userMgrService.getUserById(userId);
				if(currentUser != null){
					String nodeIdsStr = currentUser.getExt2();
					WhereCondition dmParameter = new WhereCondition();
					dmParameter.where();
					if(StrUtil.isNotBank(nodeIdsStr)){
						String[] nodeIds = nodeIdsStr.split(",");
						dmParameter.in("t2.node_id", nodeIds);
						dmParameter.andIn("t3.node_id", nodeIds);
					}else{
						dmParameter.eq("1", "2");
					}
					List<String> dmids = dataMappingService.getDmid(dmParameter);
					//过滤用户数据
					if (dmids!=null && dmids.size()>0){
						taskParameter.and().in("t2.mp_id",dmids);
					}else {
						taskParameter.and().eq("1","2");
					}
				}
			}
			List<TaskSyncStatVo> tasks = taskSyncStatService.getTaskStatData(taskParameter,dateScope, date);
			
			//set categories
			Categories cate = new Categories();
			List<Category> cList = new ArrayList<Category>();
			for(TaskSyncStatVo vo: tasks) {
				cList.add(new Category(vo.getTaskName()));
			}
			cate.setCategory(cList);
			
			String cateName = "";
			if(dataScope == 0) {
				cateName = "频次";
			} else {
				cateName = "数据量";
			}
			ChartDataset synDataSet = new ChartDataset(cateName);
			synDataSet.setColor("0000FF");
			
			//data
			List<ChartSet> chartSetList = new ArrayList<ChartSet>();
			if(dataScope == 0) {
				for(TaskSyncStatVo vo: tasks) {
					chartSetList.add(new ChartSet(""+vo.getTotal()));
				}
			} else {
				for(TaskSyncStatVo vo: tasks) {
					chartSetList.add(new ChartSet(""+vo.getDataSize()));
				}
			}
			
			synDataSet.setSet(chartSetList);
			
			//add values to chart
			List<ChartDataset> dataSetList = new ArrayList<ChartDataset>();
			dataSetList.add(synDataSet);
			 
			String xml = ChartXmlUtil.getXmlForColumn(date + " ETL任务执行" + cateName, cate, dataSetList, "次");
				
			return jsonSuccessDataResult("获取统计图", xml);
		} catch(ServiceException e) {
			logger.error(e);
			return jsonFailResult("获取统计图失败");
		}
	}
}
